﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace FzWeb.Models
{
    //留言状态说明：status 0--未审核 1--已审核
    //留言类型说明：type   0--留言,  1-- 意见
    public class OpinionService
    {
        public Entities db
        {
            get
            {
                if (HttpContext.Current.Session["db"] == null)
                {
                    HttpContext.Current.Session["db"] = new Entities();
                }
                return HttpContext.Current.Session["db"] as FzWeb.Models.Entities;
            }
        }

        public IList<Opinion> GetOpinionList()
        {
            return db.Opinion.ToList();
        }

        public IList<Opinion> GetOpinionList(Func<Opinion,bool> predict)
        {
            return db.Opinion.Where(predict).ToList();
        }

        public void CreateOpinion(Opinion e)
        {
            e.SendTime = DateTime.Now;
            e.Status=false;
            e.IsShow = true;
            db.AddToOpinion(e);
            db.SaveChanges();
        }

        public Opinion GetOpinion(Func<Opinion, bool> predicate)
        {
            return db.Opinion.First(predicate);
        }

        public Opinion GetOpinion(int id)
        {
            return db.Opinion.First(c=>c.OpinionId == id);
        }

        public void UpdateOpinion(Opinion e)
        {
            var originalOpinion = db.Opinion.Where(c => c.OpinionId == e.OpinionId).FirstOrDefault();
            db.ApplyPropertyChanges(originalOpinion.EntityKey.EntitySetName, e);
            db.SaveChanges();
        }

        public void DeleteOpinion(Opinion e)
        {
            db.DeleteObject(e);
            db.SaveChanges();
        }

        public void saveChanges()
        {
            db.SaveChanges();
        }
    }
}
